package com.mall.manager.singletopicday.service;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mall.untils.Page;
import com.mall.untils.StringUtil;

import org.springframework.web.util.WebUtils;

@Service
public class SingleTopicDayService {
	@Resource
    private JdbcTemplate jdbcTemplate;
	//日志信息
	private Logger logger = LoggerFactory.getLogger(SingleTopicDayService.class);
	
	/**
	 * 单题按天统计表信息分页方法
	 * @param request
	 * @return
	 */
	public Page manager_singleTopicDay_list(HttpServletRequest request) {
		Page page = null;
	    Map<String,Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    int pageNo = MapUtils.getIntValue(paraMap, "currentpageno");
		String topic_name = MapUtils.getString(paraMap, "topic_name");
		String str = "";
		if(!StringUtils.isEmpty(topic_name)){
			str = StringUtil.getByteToString(topic_name);			
		}
	    int pageSize = 10;
    	try {
 	        if (pageNo == 0){
 	        	pageNo = 1;
 	        }
 	       String sql = "";
 	        if(!StringUtils.isEmpty(str)){
 	        	sql = "select count(*) from db_processing.t_single_topic_day where 1=1 and topic_name like '%" + str + "%'";
 	        }else{
 	        	sql = "select count(*) from db_processing.t_single_topic_day";
 	        }
 	         
 	       int total = this.jdbcTemplate.queryForObject(sql, Integer.class);
 	        int startIndex = Page.getStartOfPage(pageNo, pageSize);
 	        StringBuffer sb = new StringBuffer("select * from db_processing.t_single_topic_day where 1=1");
 	        if(!StringUtils.isEmpty(str)){
 	        	sb.append(" and topic_name like '%" + str + "%'");
 	        }
 	        sb.append(" order by id desc limit " + startIndex + "," + pageSize);
 	        List<Map<String, Object>>  queryForList = this.jdbcTemplate.queryForList(sb.toString());
 	        page = new Page(startIndex, total, pageSize, queryForList);
 	        return page;
 	    } catch (Exception e) {
 	        this.logger.info(e.toString());
 	        return null;
 	    }
	}
	
	/**
     * @单题按天统计表信息增加
     * @param paraMap
     * @return
     */
	public int manager_insert_singleTopicDay(Map<String, Object> paraMap) {
		String topic_sum = MapUtils.getString(paraMap, "topic_sum");
		String topic_avgtime = MapUtils.getString(paraMap, "topic _avgtime");
		try {
			String sql="insert into t_users(id,topic_id,topic_sum,topic_avgtime,create_time,update_time) values(?,?,?,?,now(),now())";
			int update = jdbcTemplate.update(sql,topic_sum,topic_avgtime);
			return update;
		} catch (Exception e) {
			return 0;
		}
	}
}
